public class Foo { @Inject Instance<Bar> barInstance; public void pingNewBar() { Bar bar = barInstance.get(); bar.ping(); } }
As an alternative to using the bean manager to dynamically create beans, this can be accomplished in a type-safe way by injecting a javax.enterprise.inject.Instance<T>.
For instance, assume you have a dependent-scoped bean Bar and consider the following:
public class Foo { @Inject Instance<Bar> barInstance; public void pingNewBar() { Bar bar = barInstance.get(); bar.ping(); } }
In this example, calling barInstance.get() returns a new instance of the dependent-scoped bean Bar.